package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentUserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAgentUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AgentUserListLogic {
	return &AgentUserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 销氪绑定c端用户列表
func (l *AgentUserListLogic) AgentUserList(in *agentClient.AgentUserListReq) (*agentClient.AgentUserListResp, error) {
	list, count, err := model.AgentUserFindList(l.svcCtx.DB, in.AgentId, in.Status, in.SourceType, in.Page, in.PageSize)
	if err != nil {
		l.Error("数据库查询错误", err)
		return &agentClient.AgentUserListResp{
			Base: &agentClient.BaseResp{
				Code: agentClient.RespCode_ErrorDB,
				Msg:  err.Error(),
			},
		}, nil
	}
	res := make([]*agentClient.AgentUser, 0)
	for _, user := range list {
		temp := &agentClient.AgentUser{}
		err := copier.Copy(&temp, user)
		if err != nil {
			l.Error("copier.Copy", err)
			continue
		}
		temp.CreateTime = user.CreateTime.Unix()
		res = append(res, temp)
	}

	return &agentClient.AgentUserListResp{
		Base: &agentClient.BaseResp{
			Code: agentClient.RespCode_OK,
			Msg:  "success",
		},
		Data: &agentClient.AgentUserListResp_Data{
			Total: count,
			List:  res,
		},
	}, nil

	//count := l.svcCtx.AgentUserModel.FindListCount(l.ctx, in.AgentId, in.Status)

	//res := make([]*agentClient.AgentUser, 0)
	//if count > 0 {
	//	list, err := l.svcCtx.AgentUserModel.FindList(l.ctx, &requestx.ListReq{
	//		Page:     in.Page,
	//		PageSize: in.PageSize,
	//	}, in.AgentId, in.Status)
	//	if err != nil {
	//		l.Error("数据库查询错误", err)
	//		return &agentClient.AgentUserListResp{
	//			Base: &agentClient.BaseResp{
	//				Code: agentClient.RespCode_ErrorDB,
	//				Msg:  err.Error(),
	//			},
	//		}, nil
	//	}
	//	for _, agent := range list {
	//		temp := &agentClient.AgentUser{}
	//		err := copier.Copy(&temp, agent)
	//		if err != nil {
	//			l.Error("copier.Copy", err)
	//			return &agentClient.AgentUserListResp{
	//				Base: &agentClient.BaseResp{
	//					Code: agentClient.RespCode_ErrorSystem,
	//					Msg:  err.Error(),
	//				},
	//			}, nil
	//		}
	//		temp.CreateTime = agent.CreateTime.Unix()
	//		res = append(res, temp)
	//	}
	//}
	//
	//return &agentClient.AgentUserListResp{
	//	Base: &agentClient.BaseResp{
	//		Code: agentClient.RespCode_OK,
	//		Msg:  "success",
	//	},
	//	Data: &agentClient.AgentUserListResp_Data{
	//		Total: count,
	//		List:  res,
	//	},
	//}, nil
}
